package com.samsung.service.impl.vote;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.samsung.dao.vote.VoteDataMapper;
import com.samsung.service.vote.VoteService;

/**
 * @ClassName: TestServiceImpl
 * @Description: TODO(查询数据处理)
 * @author A18ccms a18ccms_gmail_com
 * @date 2015-4-24 下午2:08:12
 * 
 */
@Scope("prototype")
@Service("voteService")
public class VoteServiceImpl implements VoteService {
	
	private int nowPage = 1; //当前页默认为首页1
	private int pageSize = 5; // 每页数据展现行数
	private int totalRecord; // 总记录数
	private int totalPage; // 总页数

	@Autowired
	private VoteDataMapper userMapper;

	public Map<String, Object> getVoteData(Map<String, Object> map) {
		Map<String, Object> returnMap = new HashMap<String,Object>();
		List<Map<String, Object>> dataList = null;
		Object pageNo = null;
		if (map != null && map.size() >= 1) {
			pageNo = map.get("nowPage");
			if (pageNo != null) {
				nowPage = Integer.parseInt((String) pageNo);
			}else{
				nowPage=1;
			}
		}
		
		
		totalRecord = getTotalRecord(map);
		totalPage = this.getTotalPages();
		 if(totalPage==1){
	        	nowPage=1;
	        }
		dataList = this.getAll(map);
		Object select_search = map.get("select_search");
		if (select_search == null || "null".equals(select_search)) {
			select_search = "";
		} else {
			select_search = (String) select_search;
		}
		Object title_search1 = map.get("title_search");
		String title_search="";
		if (title_search1 == null || "null".equals(title_search1)) {
			title_search = "";
		} else {
			title_search = (String) title_search1;
		}
       
		returnMap.put("nowPage", nowPage);
		returnMap.put("select_search", select_search);
		returnMap.put("title_search", title_search);
		returnMap.put("totalRecord", totalRecord);
		returnMap.put("totalPage", totalPage);
		returnMap.put("dataList", dataList);

		return returnMap;
	}

	public List<Map<String, Object>> getAll(Map<String, Object> map) {
		int startRow = this.getStartRow(nowPage);
		map.put("startRow", startRow);
		map.put("pageSize", pageSize);
		List<Map<String, Object>> dataList = null;
		dataList = (List<Map<String, Object>>) userMapper.selectAll(map);
		return dataList;
	}

	/**
	 * @Title: getStartRow
	 * @Description: TODO(根据当前页获取数据开始行)
	 * @param @param pageNO
	 * @param @return 设定文件
	 * @return int 返回类型
	 * @throws
	 */
	public int getStartRow(int pageNO) {
		int startRow = 0;
		if (pageNO > 1) {
			startRow = (pageNO - 1) * pageSize;
		}
		return startRow;
	}

	public int getTotalPages() {
		totalPage = totalRecord % pageSize == 0 ? totalRecord / pageSize
				: totalRecord / pageSize + 1;
		return totalPage;
	}

	public int getTotalRecord(Map<String, Object> map) {
		totalRecord = userMapper.selectCount(map);
		return totalRecord;
	}
	

	public boolean checkVoteData(String tid){
		 List<Map<String, Object>> resultList=null;
		 boolean flag=false;
		 resultList=userMapper.getVoteHistory(tid);
		 if(resultList!=null&&resultList.size()>=1){
			 flag=false;
		 }else{
			 flag=true;
		 }	
		return flag;
	}

	@Override
	public Map<String, String> deleteVoteRecord(String tid) {
		boolean f=this.checkVoteData(tid);
		Map<String, String> returnMap=new HashMap<String, String>();
		if(!f){
			returnMap.put("flag", "failure");
			returnMap.put("message", "该话题正在被投票中，无法删除！");
		}else{
			try {
				//删除投票标题对应的选项内容
				userMapper.deleteVoteOptoionById(tid);
				//删除投票标题对应内容
				userMapper.deleteVoteTpById(tid);
				returnMap.put("flag", "success");
				returnMap.put("message", "投票话题删除成功！");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				returnMap.put("flag", "failure");
				returnMap.put("message", "投票话题删除成失败！");
				e.printStackTrace();
			}
		}
		// TODO Auto-generated method stub
		return returnMap;
		
	}
}
